package com.hup.utils.swing.customView;

import com.hup.utils.commons.start.LookThemeUtil;
import com.hup.utils.swing.DimensionUtil;
import com.hup.utils.swing.HtmlUtil.LineWrapHtmlAppender;
import lombok.Getter;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

/**
 * 选框在上,文字在下的复选框(图案文字紧凑形的)
 *
 * @author hugan
 * @date 2019/12/8
 */
public class VerticalCheckBox extends JCheckBox {

    private final int checkBoxHeight;

    /**
     * 换行文本原文;实现文本换行且居中功能
     */
    @Getter
    private String warpAbleText;

    public VerticalCheckBox() {
        //紧凑
        setIconTextGap(0);
        setFont(new Font(getFont().getName(), getFont().getStyle(), (int) (12 * LookThemeUtil.getScreenScale())));
        setBorder(new EmptyBorder(0, 0, 0, 0));
        //对齐
        setHorizontalTextPosition(SwingConstants.CENTER);
        setVerticalTextPosition(SwingConstants.BOTTOM);
        Dimension preferredSize = getPreferredSize();
        checkBoxHeight = preferredSize.height;

        //setBackground(Color.CYAN);
    }

    public void setWarpAbleText(String warpAbleText) {
        this.warpAbleText = warpAbleText;
        String[] split = warpAbleText.split("\n");
        String text = LineWrapHtmlAppender.instance(SwingConstants.CENTER).append(split).getHtml();
        setText(text);
        Dimension dimension = DimensionUtil.getSizeByWrapText(this, split);
        dimension.height += checkBoxHeight;
        Insets insets = getInsets();
        dimension.height += (insets.top + insets.bottom);
        dimension.width += (insets.left + insets.right);
        setMinimumSize(dimension);
        setPreferredSize(dimension);
    }

}
